#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        btn=WaitforElementById(driver,'com.tencent.android.qqdownloader:id/ax5',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()
    time.sleep(2)

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.tencent.android.qqdownloader:id/u1",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            try:
                clear_btn=WaitforElementById(driver,'com.tencent.android.qqdownloader:id/a32',30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                clear_btn.click()
            time.sleep(2)
    driver.back()
    #driver.find_element_by_name("分类").click()

def clickappdown(driver):
    try:
        btns=WaitforElementsById(driver,"com.tencent.android.qqdownloader:id/wt",30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for v in [0,1,2,3]:
            btns[v].click()
            time.sleep(2)
            btns[v].click()
    #driver.back()
def game(driver):
    gamecatetory = [u'休闲益智',u'网络游戏',u'棋牌中心',u'电子竞技',u'动作冒险',u'经营策略']
    try:
        btn=WaitforElementByName(driver,u'游戏',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementByName(driver,u'分类',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    time.sleep(2)
    a2=random.randint(0,8)
    print a2
    swipeUp(driver,0.9,a2*0.1)
    time.sleep(2)

    for k in gamecatetory:
        print "Current page is:",k

        try:
            btn=WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
        time.sleep(2)
        try:
            clickappdown(driver)
        except NoSuchElementException:
            driver.back()
            continue
        driver.back()
    driver.back()

def soft(driver):
    gamecatetory= [u'视频',u'阅读',u'社交',u'理财',u'购物',u'工具',u'摄影',u'音乐',u'生活',u'教育',u'出行',u'系统', \
                   u'健康',u'美化',u'旅游',u'办公',u'特色分类']
    try:
        btn=WaitforElementByName(driver,u'软件',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementByName(driver,u'分类',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    time.sleep(2)
    a2=random.randint(0,8)
    print a2
    swipeUp(driver,0.9,a2*0.1)
    time.sleep(2)

    for k in gamecatetory:
        print "Current page is:",k

        try:
            btn=WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
        time.sleep(2)
        try:
            clickappdown(driver)
        except NoSuchElementException:
            driver.back()
            continue
        driver.back()
    driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION            # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.tencent.android.qqdownloader'    # apk的包名
    desired_caps['appActivity'] = 'com.tencent.assistant.activity.SplashImplActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    #driver.implicitly_wait(30)
    time.sleep(2)
    del_appdown_task(driver)
    game(driver)
    #driver.back()
    soft(driver)
    driver.quit()

#run()